<template>
    <div class="wrapper">
        <el-card class="login">
            <div class="title">登录</div>
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item style="margin: 30px 0" prop="username">
                    <el-input v-model.trim="form.username" placeholder="账号"/>
                </el-form-item>
                <el-form-item style="margin: 30px 0" prop="password">
                    <el-input v-model.trim="form.password" placeholder="密码" @keyup.enter.native="login" type="password"/>
                </el-form-item>
                <el-form-item style="margin: 30px 0" prop="captcha" v-if="captcha.content">
                    <div style="display: flex">
                        <el-input v-model.trim="form.captcha" placeholder="验证码" @keyup.enter.native="login"/>
                        <img :src="captcha.content" @click="refreshCaptcha" style="margin-left: 10px">
                    </div>
                </el-form-item>
                <div style="margin: 30px 0">
                    <el-button style="width:100%" type="primary" @click="login" :loading="loading">登录</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    import axios from 'axios'
    import Token from '../token'

    export default {
        data() {
            return {
                form: {username: null, password: null, captcha: null},
                captcha: {content: null, signature: null},
                rules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                    captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}]
                },
                loading: false
            }
        },
        async created() {
            document.title = '登录';
            const config = await axios.get('/config');
            config.captcha && this.refreshCaptcha()
        },
        methods: {
            async refreshCaptcha() {
                this.captcha = await axios.get('captcha');
            },
            login() {
                this.$refs['form'].validate(async valid => {
                    if (!valid) return;
                    this.loading = true;
                    this.form.signature = this.captcha.signature;
                    try {
                        const result = await axios.post('/login', this.form);
                        Token.set(result.token);
                        localStorage.setItem('username', this.form.username);
                        this.$router.push('/')
                    } catch (e) {
                    }
                    this.loading = false;
                })
            }
        }
    }
</script>

<style scoped>
    .wrapper {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .login {
        width: 400px;
    }
    .login .title {
        letter-spacing: 4px;
        color: #409EFF;
        font-size: 1.6em;
        text-align: center;
    }
</style>
